import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:mall/core/models/account.dart';
import 'package:mall/core/services/api.dart';
import 'package:provider/provider.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

/// 账户密码修改
class AccountPasswordChange extends StatefulWidget {
  static const routeName = '/AccountPasswordChange';
  final Account args;

  const AccountPasswordChange({
    Key key,
    @required this.args,
  }) : super(key: key);

  @override
  _AccountPasswordChangeState createState() => _AccountPasswordChangeState();
}

class _AccountPasswordChangeState extends State<AccountPasswordChange> {
  GlobalKey<FormState> _formKey = GlobalKey();
  String _oldPassword;
  String _newPassword;
  String _newPassword2;
  bool _checked = false;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("修改保护密码"),
      ),
      body: Form(
        key: _formKey,
        child: Container(
          padding: EdgeInsets.all(8.w),
          color: Colors.white,
          child: SingleChildScrollView(
            child: Column(
              children: [
                TextFormField(
                  decoration: InputDecoration(
                    hintText: "原保护密码（${widget.args.accountTypeName}）",
                    filled: true,
                  ),
                  obscureText: true,
                  onSaved: (value) => _oldPassword = value,
                ),
                SizedBox(height: 8.w),
                TextFormField(
                  decoration: InputDecoration(
                    hintText: "新密码",
                    filled: true,
                  ),
                  obscureText: true,
                  onSaved: (value) => _newPassword = value,
                ),
                SizedBox(height: 8.w),
                TextFormField(
                  decoration: InputDecoration(
                    hintText: "确认密码",
                    filled: true,
                  ),
                  obscureText: true,
                  onSaved: (value) => _newPassword2 = value,
                ),
                Row(
                  children: [
                    Checkbox(
                      value: _checked,
                      onChanged: (value) => setState(() {
                        _checked = value;
                      }),
                    ),
                    Text('支付时是否免密'),
                  ],
                ),
                SizedBox(
                  width: double.infinity,
                  child: RaisedButton(
                    onPressed: _accountPasswordChange,
                    child: Text("确定"),
                  ),
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }

  /// 修改保护密码
  void _accountPasswordChange() async {
    _formKey.currentState.save();
    EasyLoading.show();
    final api = context.read<Api>();
    try {
      await api.userAccountPasswordChange(
        widget.args.accountType,
        _oldPassword,
        _newPassword,
        _newPassword2,
        !_checked,
      );
      EasyLoading.showToast('修改成功');
      Navigator.of(context).pop(true);
    } on DioError catch (e) {
      EasyLoading.showToast("${e.message}");
    }
  }
}
